package org.jsola.contract.web;

import org.jsola.common.DateKit8;
import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.query.ContractQuery;
import org.jsola.contract.service.IContractOverviewService;
import org.jsola.contract.service.IContractService;
import org.jsola.contract.vo.*;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * 合同总览
 *
 * @author huangrun
 */
@RestController("contractContractOverviewController")
@RequestMapping(ContractConstants.API_PATH + "/v1/overview")
public class ContractOverviewController implements IContractOverviewControllerAPI{

    @Autowired
    private IContractOverviewService contractOverviewService;

    /**
     * 合同类型 电子合同
     */
    private static final Integer CONTRACT_TYPE = 1;

    /**
     * 公司id 默认 null
     */
    private static final String SUBJECT_ID = null;

    @Autowired
    private IContractService contractService;

    @Override
    @PostMapping(value = "/selectContractCountWeek")
    public Result<List<ContractOfSignForListVO>> selectContractCountWeek(@RequestBody ContractQuery contractQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // 当前周的开始时间和结束时间的构造
        Calendar calendar = Calendar.getInstance();
        int min = calendar.getActualMinimum(Calendar.DAY_OF_WEEK);
        int current = calendar.get(Calendar.DAY_OF_WEEK);
        calendar.add(Calendar.DAY_OF_WEEK, min-current+1);
        Date start = calendar.getTime();
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        Date end = calendar.getTime();
        return Result.success(contractOverviewService.selectContractCountDay(start, end,
                contractQuery.getSubjectIds(), tokenUser.getSiteId()));
    }

    @Override
    @PostMapping(value = "/selectContractCountMonth")
    public Result<List<ContractOfSignForListVO>> selectContractCountMonth(@RequestBody ContractQuery contractQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // 当月的开始时间和结束时间的构造
        Date start = DateKit8.getFirstDayOfCurrentMonth();
        Date end = DateKit8.getLastDayOfCurrentMonth();
        return Result.success(contractOverviewService.selectContractCountDay(start, end,
                contractQuery.getSubjectIds(), tokenUser.getSiteId()));
    }

    @Override
    @PostMapping(value = "/selectContractCountYear")
    public Result<List<ContractOfSignForListVO>> selectContractCountYear(@RequestBody ContractQuery contractQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // 全年的开始时间和结束时间的构造
        Date start = DateKit8.getFirstDayOfCurrentMonth();
        start = DateKit8.addMonths(start, -11);
        Date end = DateKit8.getLastDayOfCurrentMonth();
        return Result.success(contractOverviewService.selectContractCountMonth(start, end,
                contractQuery.getSubjectIds(), tokenUser.getSiteId()));
    }

    @Override
    @PostMapping(value = "/selectContractCountFree")
    public Result<List<ContractOfSignForListVO>> selectContractCountFree(@RequestBody ContractQuery contractQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // 自定义时间的开始时间和结束时间的构造
        if (contractQuery.getPublishDateStart() == null || contractQuery.getPublishDateEnd() == null) {
            throw new ParamException("查询开始时间和结束时间都不能为空！");
        }
        Date start = DateKit8.getFirstDayOfMonth(contractQuery.getPublishDateStart());
        Date end = DateKit8.getLastDayOfMonth(contractQuery.getPublishDateEnd());
        return Result.success(contractOverviewService.selectContractCountMonth(start, end,
                contractQuery.getSubjectIds(), tokenUser.getSiteId()));
    }

    @Override
    @GetMapping(value = "/getContractCount")
    public Result<ContractCountVO> getContractCount() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看备忘录权限校验

        return Result.success(
                contractService.getContractCount(CONTRACT_TYPE, SUBJECT_ID, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/getMonthList")
    public Result<List<ContractOfSignForListVO>> selectMonthForList() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看备忘录权限校验

        return Result.success(
                contractOverviewService.selectMonthForListCount(tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/getWeekList")
    public Result<List<ContractOfSignForListVO>> selectWeekForListCount() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看备忘录权限校验

        return Result.success(
                contractOverviewService.selectWeekForListCount(tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/getYearList")
    public Result<List<ContractOfSignForListVO>> selectYearForListCount() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看备忘录权限校验

        return Result.success(
                contractOverviewService.selectYearForListCount(tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/getTimeSlotCount/{startTime}/{endTime}")
    public Result<List<ContractOfSignForListVO>> getTwoTimePeriodsForCount(@PathVariable("startTime") String startTime,
                                                                           @PathVariable("endTime") String endTime){
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看备忘录权限校验

        return Result.success(
                contractOverviewService.selectTwoTimePeriodsForCount(startTime, endTime, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/pageAmount")
    public Result<Page<ContractListVO>> page(ContractQuery contractQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                contractOverviewService.selectPage(contractQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("getComCount")
    public Result<Page<ContractStatisticsEmpUnitsVO>> selectComCountPage(ContractQuery contractQuery){
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                contractOverviewService.selectComForRankCount(contractQuery,tokenUser.getSiteId())
        );
    }
}